import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
import './element-variables.scss';
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import TokenCache from '@/utils/cache/TokenCache'
// 导出Excel文件组件
import JsonExcel from 'vue-json-excel';
Vue.component('downloadExcel', JsonExcel)

Vue.config.productionTip = false
Vue.use(ElementUI, {
  size: 'small'
});
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  document.title = ``;
  var menu = localStorage.getItem('ms_menu');
  const token = TokenCache.getToken();
  if(token){
      next()
  }else{
      if (to.path == '/login') {
          next()
      } else if (!menu && to.path !== '/login') {
          next('/login');
      } else if (menu) {
          // console.log(menu)
          var lock = false
          var menuJson = JSON.parse(menu)
     
          menuJson.forEach(function (item, index) {
              if (item.children != undefined) {
                  item.children.forEach(function (val, key) {
                     
                      if (to.path == '/' + val.path) {
                          lock = true
                          return false
                      }
                  })
              }
          })
          if (lock == false) {
              localStorage.removeItem("ms_menu")
              localStorage.removeItem("userName")
           
              next('/login')
          } else {
              next()
          }
      } else {
          next();
      }


  }

});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
